package com.example.mobile2021606145zbq0618;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class UsersRViewAdapter extends RecyclerView.Adapter<UsersRViewAdapter.ViewHolder> {
    private List<Users> list=new ArrayList<>();

    public UsersRViewAdapter(List<Users> list) {
        this.list = list;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvItemId, tvItemName, tvItemAge, tvItemScore;
        Button btnItemUpdate, btnItemDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvItemId = itemView.findViewById(R.id.tvItemId);
            tvItemAge = itemView.findViewById(R.id.tvItemAge);
            tvItemName = itemView.findViewById(R.id.tvItemName);
            tvItemScore = itemView.findViewById(R.id.tvItemScore);
            btnItemDelete = itemView.findViewById(R.id.btnMainDelete);
            btnItemUpdate = itemView.findViewById(R.id.btnMainUpdate);
        }


    }

    @NotNull
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_main_user, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
        Users users = list.get(position);
        holder.tvItemId.setText(String.valueOf(users.get_id()));
        holder.tvItemAge.setText(String.valueOf(users.getAge()));
        holder.tvItemScore.setText(String.valueOf(users.getScore()));
        holder.tvItemName.setText(String.valueOf(users.getName()));
        holder.btnItemDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyDateAdapter().delete(v.getContext(), list.get(position).get_id());
                list.remove(position);
                notifyDataSetChanged();

            }
        });
        holder.btnItemUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View view= LayoutInflater.from(v.getContext()).inflate(R.layout.dialog_insert,null);
                final EditText edtTxtDialogName = view.findViewById(R.id.edtTxtDialogName);
                final EditText edtTxtDialogAge = view.findViewById(R.id.edtTxtDialogAge);
                final EditText edtTxtDialogScore = view.findViewById(R.id.edtTxtDialogScore);
                edtTxtDialogName.setText(list.get(position).getName());
                edtTxtDialogAge.setText(String.valueOf(list.get(position).getAge()));
                edtTxtDialogScore.setText(String.valueOf(list.get(position).getScore()));
                new AlertDialog.Builder(v.getContext())
                        .setTitle("更新数据")
                        .setView(view)
                        .setPositiveButton("更新", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String name = edtTxtDialogName.getText().toString().trim();
                        int age = Integer.valueOf(edtTxtDialogAge.getText().toString().trim());
                        float score = Float.valueOf(edtTxtDialogScore.getText().toString().trim());
                        new MyDateAdapter().update(v.getContext()
                                , list.get(position).get_id()
                                , new Users(name,age,score));
                        list.clear();
                        list.addAll(new MyDateAdapter().QueryAll(v.getContext()));
                        notifyDataSetChanged();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).create().show();

            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

}

